package store.aixx.jwt.demo.security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import store.aixx.jwt.demo.entity.User;
import store.aixx.jwt.demo.repository.UserRepository;

import java.util.Optional;

/**
 * @author yukai
 * @since 2021年12月11日 0:16
 */
@Component
public class MyUserDetailsService implements UserDetailsService {

    private UserRepository userRepository;



    @Lazy
    @Autowired
    public void setUserRepository(UserRepository userRepository){
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Optional<User> userByUsername = userRepository.findUserByUsername(username);
        return userByUsername.orElse(null);
    }
}
